/**
 *@Description: 登录store
 *@author: Wangsenyang
 *@Date: 2020/2/27
 */

import api from '@/api/index';

const stateFactory = () => ({
  hasPoint: false,
  outPoint: false,
  viewFoot: {}, // 当前显示哪一个表单
  sourceInfo: {}, // 接入源信息
  dictionaryList: {}, // 字典信息
  operatorInfo: {}, // 回显用个算子信息
  submitInfo: {}, // 提交用的算子信息
  wrapInfo: {}, // 提交外层信息
  readOnlyFlag: false,
  isLink: false,
  delFlag: false, // 删除标志
});

const initState = {
  ...stateFactory(),
};

const getters = {
  getHasPoint(state) {
    return state.hasPoint;
  },
  getOutPoint(state) {
    return state.outPoint
  },
  getViewFoot(state) {
    return state.viewFoot
  },
  // 获取接入源信息
  getSourceInfo(state) {
    return state.sourceInfo
  },
  // 字典信息
  getDictionaryList(state) {
    return state.dictionaryList
  },
  // 算子信息
  getOperatorInfo(state) {
    return state.operatorInfo
  },
  // 提交算子信息
  getSubmitInfo(state) {
    return state.submitInfo
  },
  // 提交对象外层数据
  getWrapInfo(state) {
    return state.wrapInfo
  },
  getReadOnlyFlag(state) {
    return state.readOnlyFlag
  },
  getIsLink(state) {
    return state.isLink
  },
  getDelFlag(state) {
    return state.delFlag
  }
};

const actions = {
  async getList(context) {
    const params = ['BIGDATA:ASSET:ZTDX', 'BIGDATA:ASSET:SJZYLYLX', 'BIGDATA:ASSET:TYPEID', 'BIGDATA:ASSET:YWLX', 'BIGDATA:ASSET:SJJLMGJB', 'BIGDATA:ASSET:SJLYXTFLBM', 'BIGDATA:ASSET:BusinessLevel', 'BIGDATA:ASSET:ColumnType', 'BIGDATA:ASSET:XZQHFBM', 'BIGDATA:ASSET:BCJLX']
    const res = await api.home.dictionaryList(params)
    if (res.data.resultCode === 0) {
      context.commit('setDictionaryList', res.data.data)
    }
  },
};

const mutations = {
  setReset(state) {
    state.hasPoint = false
    state.outPoint = false
    state.viewFoot = {} // 当前显示哪一个表单
    state.sourceInfo = {} // 接入源信息
    state.dictionaryList = {} // 字典信息
    state.operatorInfo = {} // 回显用个算子信息
    state.submitInfo = {} // 提交用的算子信息
    state.wrapInfo = {} // 提交外层信息
    state.readOnlyFlag = false
    state.isLink = false
    state.delFlag = false // 删除标志
  },
  outPutReset(state) {
    state.hasPoint = false
    state.outPoint = false
    state.viewFoot = {} // 当前显示哪一个表单
    state.sourceInfo = {} // 接入源信息
    state.operatorInfo = {} // 回显用个算子信息
    state.submitInfo = {} // 提交用的算子信息
    state.wrapInfo = {} // 提交外层信息
    state.isLink = false
    state.delFlag = false // 删除标志
  },
  setHasPoint(state, point) {
    state.hasPoint = point;
  },
  setOutPoint(state, point) {
    state.outPoint = point;
  },
  setViewFoot(state, type) {
    state.viewFoot = type;
  },
  setSourceInfo(state, obj) {
    state.sourceInfo = obj;
  },
  setDictionaryList(state, data) {
    state.dictionaryList = data;
  },
  setOperatorInfo(state, option) {
    state.operatorInfo = Object.assign(state.operatorInfo, option);
  },
  setSubmitInfo(state, option) {
    state.submitInfo = Object.assign(state.submitInfo, option);
  },
  delOperatorInfo(state, arr) {
    arr.forEach((item) => {
      delete state.operatorInfo[item]
    })
  },
  delSubmitInfo(state, arr) {
    arr.forEach((item) => {
      delete state.submitInfo[item]
    })
  },
  setWrapInfo(state, option) {
    state.wrapInfo = Object.assign(state.wrapInfo, option);
  },
  setReadOnlyFlag(state, option) {
    state.readOnlyFlag = option
  },
  setViewInfo(state, data) {
    state.submitInfo = {}
    state.operatorInfo = {}
    state.viewFoot = {}
    const obj1 = JSON.parse(JSON.stringify(data))
    const obj2 = JSON.parse(JSON.stringify(data))
    const submitObj = {}
    obj1.accessCollectTaskOperate.forEach((item) => {
      submitObj[item.orderNumber] = item
    })
    const wrapObj = obj2
    delete wrapObj.accessCollectTaskOperate
    state.submitInfo = submitObj// 赋值给submit算子
    state.wrapInfo = wrapObj// 赋值给外层算子
    // 给接入源信息
    const obj3 = JSON.parse(JSON.stringify(data))

    obj3.columnInfo = obj3.accessCollectTaskOperate[0].outPut
    delete obj3.accessCollectTaskOperate
    state.sourceInfo = obj3
  },
  setEmpty(state) {
    state.hasPoint = false,
      state.outPoint = false,
      state.viewFoot = {}, // 当前显示哪一个表单
      state.sourceInfo = {}, // 接入源信息
      state.operatorInfo = {}, // 回显用个算子信息
      state.submitInfo = {}, // 提交用的算子信息
      state.wrapInfo = {}, // 提交外层信息
      state.readOnlyFlag = false
  },
  setIsLink(state, option) {
    state.isLink = option
  },
  setDelFlag(state, bool) {
    state.delFlag = bool
  }
};

export default {
  state: initState,
  actions,
  getters,
  mutations,
};
